﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace TodayWho
{
    /// <summary>
    /// Summary description for fileUploadHandler
    /// </summary>
    public class fileUploadHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {   
            string photoID = Guid.NewGuid().ToString();
            string tempFileName = photoID;
            string filepath = context.Server.MapPath("images/profilePics/" + tempFileName + ".jpg");
            using (FileStream fs = File.Create(filepath))
            {

                SaveFile(context.Request.InputStream, fs);
            }
            
            context.Response.ContentType = "text/plain";
            context.Response.Write("{'success': 'true', ");
            context.Response.Write("'path': 'images/profilePics/" + tempFileName + ".jpg'}");


            
        }

        private void SaveFile(Stream stream, FileStream fs)
        {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
            {

                fs.Write(buffer, 0, bytesRead);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}